﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;

using ERPStore.NewsLetters.Services;
using ERPStore.Extensions;

using CloudSoft.Extensions;

namespace ERPStore.Web.NewsLetters.Controllers
{
	[HandleError(View = "500")]
	public class AdminNewsLettersController : ERPStore.Web.Controllers.StoreController
	{
		public AdminNewsLettersController(
			ERPStore.NewsLetters.Services.NewsLettersService newsLettersService
			, ERPStore.Services.IAccountService accountService
			, ERPStore.Services.CryptoService cryptoService
			, ERPStore.NewsLetters.Services.TrackerService trackerService
			)
		{
			this.NewsLettersService = newsLettersService;
			this.AccountService = accountService;
			this.CryptoService = cryptoService;
			this.TrackerService = trackerService;
		}

		protected ERPStore.NewsLetters.Services.NewsLettersService NewsLettersService { get; private set; }

		protected ERPStore.Services.IAccountService AccountService { get; private set; }

		protected ERPStore.Services.CryptoService CryptoService { get; private set; }

		protected ERPStore.NewsLetters.Services.TrackerService TrackerService { get; private set; }

		public ActionResult Report(string campaignKey)
		{
			if (campaignKey.IsNullOrTrimmedEmpty())
			{
				return new EmptyResult();
			}
			var campaign = NewsLettersService.GetCampaignByKey(campaignKey);
			if (campaign == null)
			{
				return new EmptyResult();
			}
			ViewData.Model = campaign;
			return View();
		}

		public ActionResult LastOpenedEmail(string viewName, string campaignKey, int count)
		{
			if (campaignKey.IsNullOrTrimmedEmpty())
			{
				return new EmptyResult();
			}

			var campaign = NewsLettersService.GetCampaignByKey(campaignKey);
			if (campaign == null)
			{
				return new EmptyResult();
			}

			var log = TrackerService.GetLastOpenedEmail(campaign.Id, count);

			ViewBag.Count = count;
			ViewData.Model = log;
			return PartialView(viewName);
		}
	}
}
